
package com.example.customcomposedemo.customview

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import com.example.composeanimationdemo.R


/**
 *@author: njb
 *@date:  2023/4/5 23:13
 *@desc:
 */


@Composable
fun AirConditioner(animationDuration: Int = 1000) {
    var isOpen by remember { mutableStateOf(false) }
    val rotation by animateFloatAsState(
        targetValue = if (isOpen) 180f else 0f,
        animationSpec = tween(durationMillis = animationDuration)
    )
    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        Image(
            modifier = Modifier.rotate(rotation),
            contentDescription = "Air Conditioner Image",
            painter = painterResource(id = R.drawable.ic_launcher_background),
            contentScale = ContentScale.Crop
        )
        Button(
            modifier = Modifier.align(Alignment.BottomCenter),
            onClick = { isOpen = !isOpen }
        ) {
            Text(if (isOpen) "关闭" else "打开")
        }
    }
}
